<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/6/29
 * Time: 17:23
 */

namespace Biz\model;


class PhotoRelaGoods extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'obj_relate_wiki';

    const STATUS_NORMAL = 1;
    const STATUS_DEL = 0;

    const TYPE_USER_TAG = 1;//通过打标签关联
    const TYPE_WIKI_TO_SPU = 2;//wiki转spu关联
    const TYPE_ARTIFICIAL = 3;//运营人工挂spu


    public static function getRelaCounter($goods_id)
    {
        return self::count([
            'obj_type' => 1,
            'rel_obj_type' => 1,
            'goods_id' => $goods_id,
            'status' => self::STATUS_NORMAL
        ]);

    }

    public static function is_rela_goods($pic_id, array $goods_ids)
    {
        $relaGoodsIds = self::select('goods_id', [
            'obj_type' => 1,
            'rel_obj_id' => $pic_id,
            'rel_obj_type' => 1,
            'goods_id' => $goods_ids,
            'status' => self::STATUS_NORMAL
        ]);
        $Rst = [];
        foreach ($goods_ids as $goods_id) {
            $Rst[$goods_id] = in_array($goods_id, $relaGoodsIds) ? 1 : 0;
        }
        return $Rst;
    }

    public static function getAllRelateGoodsIds($picId)
    {
        $relateWikiIds = self::select('goods_id', [
            'rel_obj_id' => $picId,
            'rel_obj_type' => 1,
            'goods_id[!]' => 0,
            'wiki_id' => 0,
            'status' => self::STATUS_NORMAL
        ]);
        return $relateWikiIds;
    }

    public static function addRela($noteId, $picId, $goods_id, $uid, $type, $addtime = null)
    {
        $is_exist = self::get('*', [
            'obj_type' => 1,
            'obj_id' => $noteId,
            'rel_obj_id' => $picId,
            'rel_obj_type' => 1,
            'goods_id' => $goods_id,
        ]);

        if ($is_exist) {
            return $is_exist['status'] != self::STATUS_NORMAL ? self::update([
                'uid' => $uid,
                'type' => (integer)$type,
                'addtime' => $addtime ? $addtime : date('Y-m-d H:i:s'),
                'edittime'=>date('Y-m-d H:i:s'),
                'status' => self::STATUS_NORMAL,
            ], [
                'obj_type' => 1,
                'obj_id' => $noteId,
                'rel_obj_id' => $picId,
                'rel_obj_type' => 1,
                'goods_id' => $goods_id,
            ])->rowCount() : 0;
        } else {
            self::insert([
                'obj_id' => $noteId,
                'obj_type' => 1,
                'rel_obj_id' => $picId,
                'rel_obj_type' => 1,
                'goods_id' => $goods_id,
                'uid' => $uid,
                'type' => $type,
                'addtime' => $addtime ? $addtime : date('Y-m-d H:i:s'),
                'edittime'=>date('Y-m-d H:i:s'),
                'status' => self::STATUS_NORMAL,
            ]);
            return self::id();
        }
    }

    public static function getPhotosListByGoodsId($goods_id, $page, $pagesize)
    {
        return self::select('obj_id', [
            'goods_id' => $goods_id,
            'obj_type' => 1,
            'rel_obj_type' => 1,
            'status' => self::STATUS_NORMAL,
            'ORDER' => [
                "addtime" => "DESC",
                'id' => "DESC"
            ],
            'LIMIT' => [($page - 1) * $pagesize, $pagesize]
        ]);
    }


    public static function getGoodsInfoByPhotoId($photoId, $field = '*')
    {
        return self::select($field, [
            'obj_type' => 1,
            'obj_id' => $photoId,
            'rel_obj_type' => 1,
            'status' => self::STATUS_NORMAL,
            'goods_id[!]' => 0,
            'ORDER' => [
                "addtime" => "ASC",
            ],
        ]);
    }

    public static function getPhotoNumGroupByGoods($goodsIds)
    {
        $result = [];
        foreach ($goodsIds as $goodsId) {
            $result[$goodsId] = self::count([
                'AND' => [
                    'obj_type' => 1,
                    'rel_obj_type' => 1,
                    'goods_id' => $goodsId,
                    'status' => self::STATUS_NORMAL
                ]
            ]);
        }
        return $result;
    }

    public static function updateRelaInfos($data, $where)
    {
        return self::update($data, $where)->rowCount();
    }

    public static function relaCounter($photoId)
    {
        return self::count([
            'obj_type' => 1,
            'rel_obj_type' => 1,
            'obj_id' => $photoId,
            'obj_type' => 1,
            'status' => self::STATUS_NORMAL
        ]);
    }
}
